class ProImagesController < ApplicationController
  # GET /pro_images
  # GET /pro_images.xml
  layout "admin"
  before_filter :admin?,:except=>:list
  def index
    #@pro_images = ProImage.find(:all)
    condition=unless params[:product_id].blank? || params[:product_id]=="0"
      "where product_id=#{params[:product_id]}"
    else
      ""
    end
    @pro_images=ProImage.paginate_by_sql("select a.id,a.name,a.product_id,i.description,p.name img_name from pro_images a inner join products p on p.id=a.product_id inner join img_types i on i.id=a.img_type_id #{condition}",
      :per_page=>10,:page=>params[:page])
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @pro_images }
    end
  end

  def list
   
    condition=unless params[:product_id].blank? || params[:product_id]=="0"
      "where product_id=#{params[:product_id]}"
    else
      ""
    end
    @pro_images=ProImage.find_by_sql("select a.id,a.name,a.product_id,i.description,p.name product_name from pro_images a inner join products p on p.id=a.product_id inner join img_types i on i.id=a.img_type_id #{condition}")
    @page_title="产品图片"
    respond_to do |format|
      format.html { render :layout=>"pro_images"}
      format.xml  { render :xml => @pro_images }
    end
  end

  # GET /pro_images/1
  # GET /pro_images/1.xml
  def show
    @pro_image = ProImage.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @pro_image }
    end
  end

  # GET /pro_images/new
  # GET /pro_images/new.xml
  def new
    @pro_image = ProImage.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @pro_image }
    end
  end

  # GET /pro_images/1/edit
  def edit
    @pro_image = ProImage.find(params[:id])
    @img_type=ImgType.all
  end

  # POST /pro_images
  # POST /pro_images.xml
  def create
    #@pro_image = ProImage.new(params[:pro_image])
    flash[:notice]=""
    params[:img].each_value do |file|
      unless file.blank?
        @pro_image = ProImage.new(:image=>file,:product_id=>params[:product_id],:img_type_id=>2)
        if @pro_image.save
          flash[:notice] += "#{file.original_filename}上传成功！"
        else
          flash[:notice] += "#{file.original_filename}上传失败！"
        end
      end
    end

    respond_to do |format|        
      format.html { redirect_to(product_pro_images_path(params[:product_id])) }
    end
  end

  # PUT /pro_images/1
  # PUT /pro_images/1.xml
  def update
    @pro_image = ProImage.find(params[:id])

    respond_to do |format|
      if @pro_image.update_attributes(params[:pro_image])
        flash[:notice] = 'ProImage was successfully updated.'
        format.html { redirect_to(product_pro_images_path(@pro_image.product_id)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @pro_image.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /pro_images/1
  # DELETE /pro_images/1.xml
  def destroy
    @pro_image = ProImage.find(params[:id])
    @pro_image.destroy

    respond_to do |format|
      format.html { redirect_to(product_pro_images_path(@pro_image.product_id)) }
      format.xml  { head :ok }
    end
  end
end
